#include"WinMain.h"

LRESULT WINAPI WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch(message){
	case WM_CLOSE:
	case WM_DESTROY:
	case WM_QUIT:
		game_over = true;
		break;	
	default:
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
	MSG msg;
	srand( (unsigned int)time(NULL) );
	g_hInstance = hInstance;
	g_nCmdShow = nCmdShow;
	DWORD dwStyle, dwExStyle;
	RECT windowRect;

	//Khoi tao Engine
	g_engine = new Engine();
	g_camera = new Camera();

	if(!game_preload()){
		MessageBox(g_hWnd, "Error in game preload", "ERROR", MB_OK);	
		return 0;
	}

	//lay tieu de cho window
	char title[255];
	sprintf(title,"%s",g_engine->getAppTitle().c_str());

	//cai dat kich thuoc window
	windowRect.top = 0;
	windowRect.left = 0;
	windowRect.bottom = g_engine->getScreenHeight();
	windowRect.right = g_engine->getScreenWidth();

	//khoi tao lop cau truc window
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	wc.style = CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = title;
	wc.hIconSm = NULL;

	RegisterClassEx(&wc);

	if(g_engine->getFullScreen()){
		DEVMODE dm;
		memset(&dm, 0, sizeof(dm));
		dm.dmSize = sizeof(dm);
		dm.dmPelsWidth = g_engine->getScreenWidth();
		dm.dmPelsHeight = g_engine->getScreenHeight();
		dm.dmBitsPerPel = g_engine->getColorDepth();
		dm.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

		if(ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
			MessageBox( NULL, "Display mode filed" , NULL, MB_OK );
			g_engine->setFullScreen(false);
		}

		dwStyle = WS_POPUP;
		dwExStyle = WS_EX_APPWINDOW;
		ShowCursor(false);
	}
	else{
		dwStyle = WS_OVERLAPPEDWINDOW;
		dwExStyle = WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;
	}

	//dieu chinh window tra ve kich thuoc dung
	AdjustWindowRectEx(&windowRect, dwStyle, NULL, dwExStyle);

	//Khoi tao chuong trinh window
	g_hWnd = CreateWindowEx( 0,
							 title,
							 title,
							 dwStyle|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
							 0,0,
							 g_engine->getScreenWidth(), g_engine->getScreenHeight(),
							 NULL,
							 NULL,
							 hInstance,
							 0);

	if(!g_hWnd){
		MessageBox( g_hWnd, "Error creating program window", "ERROR", MB_OK );
		return 0;
	}

	//The hien window
	ShowWindow(g_hWnd, nCmdShow);
	UpdateWindow(g_hWnd);

	//cai dat engine
	g_engine->setWindowHandle(g_hWnd);
	if(!g_engine->Init(g_engine->getScreenWidth(), g_engine->getScreenHeight(), g_engine->getColorDepth(), g_engine->getFullScreen())){
		MessageBox(g_hWnd, "Error initializing the engine", "Error", MB_OK);
		return 0;
	}

	//main
	game_over = false;
	while(!game_over){
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		g_engine->Update();
	}

	if(g_engine->getFullScreen()){
		ShowCursor(TRUE);
	}

	g_engine->Close();
	delete g_engine;

	return 1;
}